// router.js
import { RouterMount, createRouter } from 'uni-simple-router';
import store from '../store'
import pages from '../pages.js'
import { getToken } from '../utils/auth'
const myRouter =
	pages().pages.map(item => ({
		path: `/${item.path}`,
		meta: item.meta || {},
		name: item.name
	}))
//初始化
const Router = createRouter({
	mode: 'hash',
	platform: process.env.VUE_APP_PLATFORM,
	routes: [
		...myRouter,
		{
			path: '/',
			meta: {
				needToken: true
			}
		},
	]
});
//全局路由前置守卫
Router.beforeEach((to, from, next) => {
	const token = store.getters.token || getToken()
	if (token && !to.meta.needToken) {
		next({
			path: '/'
		})
	} else if (!token && to.meta.needToken) {
		next({
			path: '/pages/login/index'
		})
	} else {
		next()
	}
})


export {
	Router,
	RouterMount
}